import { Injectable } from '@nestjs/common'

// 1. 引入3个内容
import { Like, Repository } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { Girl } from './girl.entity'

import * as dayjs from 'dayjs'

@Injectable()
export class GirlService {
  // 2. 在构造函数中 做依赖注入
  constructor(
    @InjectRepository(Girl) private readonly girl: Repository<Girl>
  ) {}

  getGirls() {
    return {
      code: 0,
      data: ['数据1', '数据2', '数据3'],
      msg: '请求成功'
    }
  }

  getGirlById(id: number) {
    const m = {
      101: { id: 101, name: '张三', age: 12 },
      102: { id: 102, name: 'adad', age: 33 },
      103: { id: 103, name: 'fgggg', age: 54 }
    }

    return {
      code: 200,
      data: m[id]
    }
  }

  /**
   * 新增
   * @param req
   * @returns
   */
  async addGirl(req) {
    const { name, skill } = req.body

    // 先查询是否有同名的

    try {
      const findRes = await this.girl.findOne({
        where: {
          name: name
        }
      })

      if (findRes) {
        return {
          code: 200,
          msg: '已经存在相同的人名'
        }
      }

      const data = new Girl()

      data.name = name.trim()
      data.skill = skill.trim()
      this.girl.save(data)

      return {
        code: 200,
        msg: '操作成功'
      }
    } catch (error) {
      return {
        code: 200,
        msg: '操作失败'
      }
    }
  }

  /**
   * 删除
   */
  async deleteGirl(params) {
    console.log(params)

    const id = params.id

    if (id === undefined) {
      return {
        code: 200,
        msg: '未查询到对应人员'
      }
    }
    const name = '张三'
    try {
      const findRes = await this.girl.find({
        where: {
          name: name
        }
      })

      console.log('查询到的结果', findRes)
      return {
        code: 200,
        msg: '删除成功'
      }

      // let res = await this.girl.delete(id)
    } catch (error) {
      return {
        code: 10001,
        msg: '删除失败'
      }
    }
  }

  /**
   * 更新人员信息
   * @param req
   */
  async updateGirl(req) {
    try {
      const { name, afterName } = req

      const findRes = await this.girl.findOne({
        where: {
          name: name
        }
      })

      // 找到了，直接更新
      if (findRes) {
        await this.girl.update(findRes.id, {
          name: afterName
        })

        return {
          code: 200,
          msg: '操作成功'
        }
      }

      return {
        code: 200,
        msg: '修改失败，修改的人不存在'
      }
    } catch (error) {
      return {
        code: 200,
        msg: '操作失败'
      }
    }
  }

  /**
   * 查询所有人员
   * @returns
   */
  async getAllPerson() {
    try {
      const res = await this.girl.find()

      const list = res.map((item) => {
        return {
          ...item,
          createTime: dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss'),
          updateTime: dayjs(item.updateTime).format('YYYY-MM-DD HH:mm:ss')
        }
      })

      return {
        code: 200,
        data: list,
        msg: '操作成功'
      }
    } catch (error) {
      console.log('报错了')
      console.log(error)

      return {
        code: 200,
        msg: '查询失败'
      }
    }
  }

  /**
   * 模糊查询
   * @param body
   * @returns
   */
  async getPerson(body) {
    const { name } = body
    console.log(name)

    try {
      const res = await this.girl.find({
        where: {
          name: Like(`${name}%`)
        }
      })

      return {
        code: 200,
        data: res || [],
        msg: '操作成功'
      }
    } catch (error) {}
  }
}
